/*
 */
package org.hook.showcase.concurrent.atomic;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

import org.testng.annotations.Test;

/**
 * @author suwuqiang 2011-12-13
 */
public class AtomicIntegerFieldUpdaterTest {
	private final FieldUpdaterData data = new FieldUpdaterData();

	@Test
	public void test() {
		data.execute(data);
	}

	class FieldUpdaterData {
		public volatile int value1 = 1;
		volatile int value2 = 2;
		protected volatile int value3 = 3;
		private volatile int value4 = 4;

		AtomicIntegerFieldUpdater<FieldUpdaterData> getUpdater(String fieldName) {
			return AtomicIntegerFieldUpdater.newUpdater(FieldUpdaterData.class, fieldName);
		}

		void execute(FieldUpdaterData data) {
			System.out.println("1 ==> " + getUpdater("value1").getAndSet(data, 10));
			System.out.println("3 ==> " + getUpdater("value2").incrementAndGet(data));
			System.out.println("2 ==> " + getUpdater("value3").decrementAndGet(data));
			System.out.println("true ==> " + getUpdater("value4").compareAndSet(data, 4, 5));
		}
	}
}
